(function ($) {
    var methods = {
    	/*初始化方法*/
        _init: function(options){
            //在每个元素上执行方法
            return this.each(function() {
                //尝试去获取settings，如果不存在，则返回“undefined”
                var settings = $(this).data('DawnLayout');
                //如果获取settings失败，则根据options和default创建它
                if(typeof(settings) == 'undefined') {
                    var defaults = {
                    	cls: null,                            //默认样式                    	
                    	locale: 'zh-CN',                      //默认语言
						direction: 'horizontal',              //有vertical(垂直)和horizontal(水平)两种方式,默认水平布局
						panes: [],                            //布局组件,必须要有id和size属性
                    	isBorder: true,                       //是否显示边框，默认显示
                    	draggable: false,                     //是否可拖动
                    	onDragStart: function(data,evt){},    //拖动触发事件
                    	onDragOver: function(evt){},          //拖动完成触发事件
                    	
                    };
                    
                    //默认语言包为中文
                    defaults.locales = [];                    
                    defaults.locales['zh-CN'] = defaults.locales['zh'] = {
                    };

                    //$.extend(defaults, defaults['zh-CN']);                    
                    settings = $.extend({}, defaults, options);  
                    //保存我们新创建的settings
                    //$(this).data('DawnLayout', settings);
                }else {
                    //如果我们获取了settings，则将它和options进行合并
                    settings = $.extend({}, settings, options);
                }
                //初始化语言
                methods._initLocale.call(this,settings);
                this.settings = settings;
                $(this).addClass("dawn-layout");
                for(var i=0;i<settings.panes.length;i++){
					var pane = settings.panes[i];
					var item = $(this).find("#"+pane.id);
					if(item.length>0){
						item.addClass("layout-block").addClass(settings.direction);
						if(settings.direction=='horizontal'){
	                        item.css('width',pane.size);
						}else{
	                        item.css('height',pane.size);
						}
					}else{
						item = $('<div id='+pane.id+' class="layout-block '+settings.direction+'"></div>');
	                    if(settings.direction=='horizontal'){
	                        item.css('width',pane.size);
						}else{
	                        item.css('height',pane.size);
						}
						$(this).append(item);
					}
					if(settings.isBorder&&i<settings.panes.length-1){
                        item.after('<div class="layout-border '+settings.direction+'"></div>');
					}
				}				
            });
        },
        
        
        /*初始化语言包内容，将其加入setting参数*/
        _initLocale: function(settings){
            if (settings.locale) {
                var parts = settings.locale.split(/-|_/);
                parts[0].toLowerCase();
                parts[1] && parts[1].toUpperCase();
                if (settings.locales[settings.locale]) {
                    $.extend(settings, settings.locales[settings.locale]);
                } else if (settings.locales[parts.join('-')]) {
                    $.extend(settings, settings.locales[parts.join('-')]);
                } else if (settings.locales[parts[0]]) {
                    $.extend(settings, settings.locales[parts[0]]);
                }
            }        	
        },
        
        /*初始化触发事件*/
        _initEvent: function(settings){
        	var that = this;
        	if(settings.draggable){
        		methods._initDragEvent.call(this,settings);
        	}            
        },
        
        /*拖拽事件*/
        _initDragEvent: function(settings){
        	var that = $(this);
        	$('tr',$(this)).on('dragstart', function(evt) {
        		var data = $(this).data('data');
        		settings.onDragStart(data,evt);
        	}).on('dragover', function(evt) {
        		evt.preventDefault();
        		settings.onDragOver(evt);
        	}).on('drop', function(evt) {
        		evt.preventDefault();
        		settings.onDrop(evt);
        	});
        },
        
    };
    
    /*定义布局函数*/
    $.fn.DawnLayout = function() {
        var method = arguments[0];  
        if(methods[method]) {
            method = methods[method];
            arguments = Array.prototype.slice.call(arguments, 1);
        } else if( typeof(method) == 'object' || !method ) {
            method = methods._init;
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.DawnLayout' );
            return this;
        }  
        return method.apply(this, arguments);  
    };

})(window.jQuery);